using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;

namespace MedicalSystem.SmartReport.Host 
{

	public class HostSurface : DesignSurface
	{
		private BasicDesignerLoader _loader;
		private ISelectionService _selectionService;
      
		public HostSurface() : base()
		{
            this.AddService(typeof(IMenuCommandService), new MenuCommandService(this));
           
		}
		public HostSurface(IServiceProvider parentProvider) : base(parentProvider)
		{
            this.AddService(typeof(IMenuCommandService), new MenuCommandService(this));
           
        }

		internal void Initialize()
		{

			
			IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));

			if (host == null)
				return;

			try
			{
				
				Type hostType = host.RootComponent.GetType();
				if(hostType==typeof(Form))
				{
                    Form form = host.RootComponent as Form;
                    form.Dock = DockStyle.Fill;
                    form.FormBorderStyle = FormBorderStyle.None;
                    form.BackColor = System.Drawing.Color.White;
                 
                   
				}				
				else
				{
					throw new Exception("Undefined Host Type: " + hostType.ToString());
				}

				_selectionService = (ISelectionService)(this.ServiceContainer.GetService(typeof(ISelectionService)));
				_selectionService.SelectionChanged += new EventHandler(selectionService_SelectionChanged);
			}
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }

		public BasicDesignerLoader Loader
		{
			get
			{
				return _loader;
			}
			set
			{
				_loader = value;
			}
		}

		
        private void selectionService_SelectionChanged(object sender, EventArgs e)
		{
			if (_selectionService != null)
			{
				ICollection selectedComponents = _selectionService.GetSelectedComponents();
              
				PropertyGrid propertyGrid = (PropertyGrid)this.GetService(typeof(PropertyGrid));

                if (selectedComponents.Count == 0)
                    propertyGrid.SelectedObjects = null;
              
                object[] comps = new object[selectedComponents.Count];
               
                foreach (Object o in selectedComponents)
                {
                    if (o.GetType() != typeof(Form))
                    {
                        CustomProperty mp = new CustomProperty(o);
                        propertyGrid.SelectedObject = mp;
                    }
                    
                }                                          
			}
		}

		public void AddService(Type type, object serviceInstance)
		{
			this.ServiceContainer.AddService(type, serviceInstance);
		}
	}
}
